package com.xju.ExampleSystem;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import javax.swing.*;

import com.sun.corba.se.spi.ior.Writeable;

public class QuestionAdd extends JFrame implements ActionListener {

	private JLabel jLabelA, jLabelB, jLabelC, jLabelD, jLabelContent,
			jLabelRight;
	private JTextField jTextFieldA, jTextFieldB, jTextFieldC, jTextFieldD,
			jTextFieldRight;
	private JTextArea jTextArea;

	private JButton buttonOK, buttonClear;

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(buttonClear)) {
			Clear();
		}
		if (e.getSource().equals(buttonOK)) {
			Add();
		}
	}

	public QuestionAdd() {
		super();
		init();
	}

	private void Add() {
		Question question = new Question();
		question.setAnswerA(jTextFieldA.getText());
		question.setAnswerB(jTextFieldB.getText());
		question.setAnswerC(jTextFieldC.getText());
		question.setAnswerD(jTextFieldD.getText());
		question.setRightAnswer(jTextFieldRight.getText());
		question.setContent(jTextArea.getText());
		write(question);
	}

	private void write(Question question) {
		try {

			FileWriter writer = new FileWriter(
					"./src/com/xju/ExampleSystem/Examples.txt", true);
			writer.write("\r" + question.toString());
			writer.flush();
			writer.close();
			Clear();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "错误",
					JOptionPane.OK_OPTION);
		}
	}

	private void Clear() {
		jTextArea.setText("");
		jTextFieldA.setText("");
		jTextFieldB.setText("");
		jTextFieldC.setText("");
		jTextFieldD.setText("");
		jTextFieldRight.setText("");
	}

	private void init() {
		setSize(400, 440);
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setName("考试系统");
		this.setTitle("考试系统");
		jLabelA = new JLabel("答案A");
		jLabelA.setBounds(10, 20, 120, 20);
		this.add(jLabelA);
		jTextFieldA = new JTextField();
		jTextFieldA.setBounds(70, 20, 220, 20);
		this.add(jTextFieldA);

		jLabelB = new JLabel("答案B");
		jLabelB.setBounds(10, 60, 120, 20);
		this.add(jLabelB);
		jTextFieldB = new JTextField();
		jTextFieldB.setBounds(70, 60, 220, 20);
		this.add(jTextFieldB);

		jLabelC = new JLabel("答案C");
		jLabelC.setBounds(10, 100, 120, 20);
		this.add(jLabelC);
		jTextFieldC = new JTextField();
		jTextFieldC.setBounds(70, 100, 220, 20);
		this.add(jTextFieldC);

		jLabelD = new JLabel("答案D");
		jLabelD.setBounds(10, 140, 120, 20);
		this.add(jLabelD);
		jTextFieldD = new JTextField();
		jTextFieldD.setBounds(70, 140, 220, 20);
		this.add(jTextFieldD);

		jLabelRight = new JLabel("准确答案");
		jLabelRight.setBounds(10, 180, 120, 20);
		this.add(jLabelRight);
		jTextFieldRight = new JTextField();
		jTextFieldRight.setBounds(70, 180, 220, 20);
		this.add(jTextFieldRight);

		jLabelContent = new JLabel("题内容");
		jLabelContent.setBounds(10, 250, 120, 20);
		this.add(jLabelContent);
		jTextArea = new JTextArea();
		jTextArea.setLineWrap(true);
		jTextArea.setBounds(70, 210, 220, 130);
		this.add(jTextArea);

		buttonOK = new JButton("添加");
		buttonOK.setBounds(80, 380, 80, 20);
		buttonOK.addActionListener(this);
		this.add(buttonOK);
		buttonClear = new JButton("清空");
		buttonClear.setBounds(170, 380, 80, 20);
		this.add(buttonClear);
		buttonClear.addActionListener(this);
	}
}
